<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('purchaseOrder:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单编码">
                <a-input v-model="queryParam.code" allow-clear placeholder="请输入订单编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单状态">
                <a-select style="width: 100%" v-model="queryParam.status" allow-clear placeholder="请选择订单状态">
                  <a-select-option v-for="(item,index) in statusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="供应商名称">
                  <a-input v-model="queryParam.supplierName" allow-clear placeholder="请输入供应商名称"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :scroll="{x: 1200}"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('purchaseOrder:add')" >
          <a-button type="primary" v-if="hasPerm('purchaseOrder:add')" icon="plus" @click="goToAddForm">新增采购订单</a-button>
        </template>
        <span slot="statusscopedSlots" slot-scope="text">
          {{ 'purchaseorder_status' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="goToViewForm(record)">查看</a>
          <a-divider type="vertical" v-if=" hasPerm('purchaseOrder:storage')"/>
          <a v-if="hasPerm('purchaseOrder:storage')" @click="goToStorageForm(record)">收货</a>
          <a-divider type="vertical" v-if="record.status === 'PENDING' && (hasPerm('purchaseOrder:edit') & hasPerm('purchaseOrder:delete'))"/>
          <a v-if="record.status === 'PENDING' && hasPerm('purchaseOrder:edit')" @click="goToEditForm(record)">编辑</a>
          <a-divider type="vertical" v-if="record.status === 'PENDING' && (hasPerm('purchaseOrder:edit') & hasPerm('purchaseOrder:delete'))"/>
          <a-popconfirm v-if="record.status === 'PENDING' && hasPerm('purchaseOrder:delete')" placement="topRight" title="确认删除？" @confirm="() => purchaseOrderDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { purchaseOrderPage, purchaseOrderDelete } from '@/api/modular/main/order/purchase/purchaseOrderManage'
  export default {
    components: {
      STable
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '订单编码',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'code'
          },
          {
            title: '供应商名称',
            align: 'center',
            width: 250,
            ellipsis: true,
            dataIndex: 'supplierName'
          },
          {
            title: '签订时间',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'signDate'
          },
          {
            title: '税率(%)',
            align: 'center',
            width: 80,
            ellipsis: true,
            dataIndex: 'taxRate'
          },
          {
            title: '含税总额',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'totalTaxPrice'
          },
          {
            title: '订单状态',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'status',
            scopedSlots: { customRender: 'statusscopedSlots' }
          },
          {
            title: '采购数',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'totalQty',
            scopedSlots: { customRender: 'totalQty' }
          },
          {
            title: '到货数',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'totalStorageQty',
            scopedSlots: { customRender: 'totalStorageQty' }
          },
          // {
          //   title: '发票金额',
          //   align: 'center',
          //   width: 100,
          //   ellipsis: true,
          //   dataIndex: 'invoiceAmount'
          // },
          {
            title: '备注',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'remark'
          },
          {
            title: '更新人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'updateUserName'
          },
          {
            title: '更新时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'updateTime'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return purchaseOrderPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        statusData: [],
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
      if (this.hasPerm('purchaseOrder:edit') || this.hasPerm('purchaseOrder:delete')) {
        this.columns.push({
          title: '操作',
          align: 'center',
          width: 200,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          fixed: 'right'
        })
      }
      const statusOption = this.$options
      this.statusData = statusOption.filters['dictData']('purchaseorder_status')
    },
    activated () {
      this.$refs.table.refresh()
    },
    methods: {
      purchaseOrderDelete (record) {
        purchaseOrderDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败:' + res.message)
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      goToAddForm () {
        this.$router.push({ path: '/purchaseOrderForm/add' })
      },
      goToEditForm (record) {
        this.$router.push({
          path: '/purchaseOrderForm/edit',
          query: {
            code: record.code
          }
        })
      },
      goToViewForm (record) {
        this.$router.push({
          path: '/purchaseOrderForm/view',
          query: {
            code: record.code
          }
        })
      },
      goToStorageForm (record) {
        this.$router.push({
          path: '/purchaseOrderForm/storage',
          query: {
            code: record.code
          }
        })
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
